package org.officelaf.options;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.plaf.basic.BasicSplitPaneUI;
import javax.swing.table.DefaultTableCellRenderer;
import org.officelaf.icons.IconProviderTableModel;
import org.officelaf.spi.IconProvider;
import org.officelaf.icons.IconService;
import org.openide.util.NbPreferences;

public final class OfficeLAFPanel extends javax.swing.JPanel {

    private final OfficeLAFOptionsPanelController controller;

    OfficeLAFPanel(OfficeLAFOptionsPanelController controller) {
        this.controller = controller;
        initComponents();
        setOpaque(false);
        iconProviderList.setCellRenderer(new DefaultListCellRenderer() {
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                return super.getListCellRendererComponent(list, value.getClass().getName(), index, isSelected, cellHasFocus);
            }
        });
        reloadButtonActionPerformed(null);
        
        // TODO listen to changes in form fields and call controller.changed()

        table.setDefaultRenderer(Dimension.class, new DefaultTableCellRenderer() {
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                if(value instanceof Dimension) {
                    Dimension d = (Dimension) value;
                    if(d != null) {
                        value = "(" + d.width + "," + d.height + ")";
                    }
                } 

                return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            }

        });

        if(splitPane.getUI() instanceof BasicSplitPaneUI) {
            BasicSplitPaneUI bsp = (BasicSplitPaneUI) splitPane.getUI();
            bsp.getDivider().setBorder(null);
            bsp.getDivider().setBackground(Color.WHITE);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        splitPane = new javax.swing.JSplitPane();
        providerScroll = new javax.swing.JScrollPane();
        iconProviderList = new javax.swing.JList();
        iconScroll = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        reloadButton = new javax.swing.JButton();
        providerIcon = new javax.swing.JLabel();
        menuCheck = new javax.swing.JCheckBox();
        toolCheck = new javax.swing.JCheckBox();
        ribbonCheck = new javax.swing.JCheckBox();

        splitPane.setBorder(null);
        splitPane.setContinuousLayout(true);
        splitPane.setOpaque(false);

        iconProviderList.setModel(new DefaultListModel());
        iconProviderList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                iconProviderListValueChanged(evt);
            }
        });
        providerScroll.setViewportView(iconProviderList);

        splitPane.setLeftComponent(providerScroll);

        table.setModel(new IconProviderTableModel());
        table.setFillsViewportHeight(true);
        iconScroll.setViewportView(table);

        splitPane.setRightComponent(iconScroll);

        org.openide.awt.Mnemonics.setLocalizedText(reloadButton, org.openide.util.NbBundle.getMessage(OfficeLAFPanel.class, "OfficeLAFPanel.reloadButton.text")); // NOI18N
        reloadButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reloadButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(providerIcon, org.openide.util.NbBundle.getMessage(OfficeLAFPanel.class, "OfficeLAFPanel.providerIcon.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(menuCheck, org.openide.util.NbBundle.getMessage(OfficeLAFPanel.class, "OfficeLAFPanel.menuCheck.text")); // NOI18N
        menuCheck.setOpaque(false);

        org.openide.awt.Mnemonics.setLocalizedText(toolCheck, org.openide.util.NbBundle.getMessage(OfficeLAFPanel.class, "OfficeLAFPanel.toolCheck.text")); // NOI18N
        toolCheck.setOpaque(false);

        org.openide.awt.Mnemonics.setLocalizedText(ribbonCheck, org.openide.util.NbBundle.getMessage(OfficeLAFPanel.class, "OfficeLAFPanel.ribbonCheck.text")); // NOI18N
        ribbonCheck.setOpaque(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(splitPane, javax.swing.GroupLayout.DEFAULT_SIZE, 541, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(menuCheck)
                        .addGap(18, 18, 18)
                        .addComponent(toolCheck)
                        .addGap(18, 18, 18)
                        .addComponent(ribbonCheck))
                    .addComponent(providerIcon)
                    .addComponent(reloadButton, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {menuCheck, ribbonCheck, toolCheck});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(menuCheck)
                    .addComponent(toolCheck)
                    .addComponent(ribbonCheck))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(providerIcon)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(splitPane, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                .addGap(11, 11, 11)
                .addComponent(reloadButton)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void reloadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reloadButtonActionPerformed

        DefaultListModel model = new DefaultListModel();
        for(IconProvider provider : IconService.getInstance().getIconProviders()) {
            model.addElement(provider);
        }
        
        iconProviderList.setModel(model);
}//GEN-LAST:event_reloadButtonActionPerformed

    private void iconProviderListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_iconProviderListValueChanged
       if(!evt.getValueIsAdjusting()) {
           IconProvider provider = (IconProvider) iconProviderList.getSelectedValue();
           if(provider != null) {
               table.setModel(new IconProviderTableModel(provider));
           } else {
               table.setModel(new IconProviderTableModel());
           }
       }
    }//GEN-LAST:event_iconProviderListValueChanged

    void load() {
        menuCheck.setSelected(  NbPreferences.forModule(OfficeLAFPanel.class).getBoolean("menuCheck",   false));
        toolCheck.setSelected(  NbPreferences.forModule(OfficeLAFPanel.class).getBoolean("toolCheck",   false));
        ribbonCheck.setSelected(NbPreferences.forModule(OfficeLAFPanel.class).getBoolean("ribbonCheck", true));
    }

    void store() {
        NbPreferences.forModule(OfficeLAFPanel.class).putBoolean("menuCheck",   menuCheck.isSelected());
        NbPreferences.forModule(OfficeLAFPanel.class).putBoolean("toolCheck",   toolCheck.isSelected());
        NbPreferences.forModule(OfficeLAFPanel.class).putBoolean("ribbonCheck", ribbonCheck.isSelected());
    }

    boolean valid() {
        // TODO check whether form is consistent and complete
        return true;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList iconProviderList;
    private javax.swing.JScrollPane iconScroll;
    private javax.swing.JCheckBox menuCheck;
    private javax.swing.JLabel providerIcon;
    private javax.swing.JScrollPane providerScroll;
    private javax.swing.JButton reloadButton;
    private javax.swing.JCheckBox ribbonCheck;
    private javax.swing.JSplitPane splitPane;
    private javax.swing.JTable table;
    private javax.swing.JCheckBox toolCheck;
    // End of variables declaration//GEN-END:variables
}
